package com.wtone.platform.gat.images.gat.service.impl;

import com.alibaba.fastjson.JSON;
import com.wtone.platform.gat.images.gat.core.Response;
import com.wtone.platform.gat.images.gat.entity.Facedb;
import com.wtone.platform.gat.images.gat.mapper.FacedbMapper;
import com.wtone.platform.gat.images.gat.message.FeignSubImageInfoCore;
import com.wtone.platform.gat.images.gat.message.SubImageInfoCore;
import com.wtone.platform.gat.images.gat.model.*;
import com.wtone.platform.gat.images.gat.service.FaceService;
import com.wtone.platform.gat.images.gat.utils.BenUtils;
import com.wtone.platform.gat.images.gat.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: aiLin
 * @Date: 2020/10/9 15:24
 * @desc: 人脸对象接口实现
 */
@Slf4j
@Service
public class FaceServiceImpl implements FaceService {

    @Autowired
    private FacedbMapper facedbMapper;

    @Autowired
    private FeignSubImageInfoCore feignSubImageInfoCore;

    /**
     * 批量人脸新增
     * @param faceListObject
     * @return
     */
    @Override
    public ResponseStatusListModeObject addFace(FaceListObject faceListObject) {
        String reqUrl = "/VIID/Faces";
        //log.info(JSON.toJSONString(faceListObject));
        List<ResponseStatus> resp3 = new ArrayList<ResponseStatus>();
        ResponseStatus r = new ResponseStatus();
        List<Face> faceList = faceListObject.getFaceObject().getFaceList();
        if(faceList!=null && faceList.size()>0){
            for(int i=0;i<faceList.size();i++){
                Face face = faceList.get(i);
                List<SubImageInfo> subImageInfo = face.getSubImageInfoList().getSubImageInfo();
                new Thread(() -> {
                    SubImageInfoCore.getSubImageInfo(subImageInfo);
                }).start();
                //new Thread(() -> {
                    feignSubImageInfoCore.getSubImageInfo(subImageInfo,faceListObject);
                //}).start();
                try {
                    Facedb facedb = (Facedb) BenUtils.beanTobeanLowerCase(face, Facedb.class);
                    facedb.setCreateBy("admin");
                    facedb.setCreateTime(DateUtils.getNowDate());
                    facedb.setSubimageinfolist(JSON.toJSONString(faceListObject));
                    facedb.setCriminalInvolvedSpeciCode(face.getCriminalInvolvedSpecilisationCode());
                    int mark = facedbMapper.insertFacedb(facedb);
                    if(mark>=1){
                        r = Response.getResponOk(facedb.getFaceid(),reqUrl,"成功");
                    }else{
                        r = Response.getResponError(facedb.getFaceid(),reqUrl,"新增失败");
                    }
                } catch (Exception e) {
                    if(String.valueOf(e).contains("唯一")){
                        log.error("人脸数据：{}，已存在，不重新记录",face.getFaceID());
                        r = Response.getResponOk(face.getFaceID(),reqUrl,"人脸数据："+face.getFaceID()+"，已存在，不重新记录");
                    }else{
                        r = Response.getResponOk(face.getFaceID(),reqUrl,"人脸数据："+face.getFaceID()+"，异常，存入记录表");
                    }
                }
            }
        }
        resp3.add(r);
        return Response.ok(resp3);
    }

    /***
     * 批量查询
     * @param FaceID
     * @param InfoKind
     * @param IDType
     * @param SourceID
     * @param DeviceID
     * @param Name
     * @return
     */
    @Override
    public ResponseStatusListModeObject seleteFace(String FaceID, String InfoKind, String IDType, String SourceID, String DeviceID, String Name) {
        Facedb face = new Facedb();
        face.setFaceid(FaceID!=null ? FaceID : "");
        facedbMapper.selectFacedbList(face);
        return null;
    }
}
